home *** CD-ROM | disk | FTP | other *** search
/ SGI Performance Co-Pilot 1.3 / SGI Performance Co-Pilot 1.3.iso / dist / pcp.idb / usr / sbin / pmafm.z / pmafm
Text File  |  1997-04-03  |  11KB  |  536 lines

  1. #!/bin/sh
  2. #
  3. # Copyright 1995, Silicon Graphics, Inc.
  4. # ALL RIGHTS RESERVED
  5. # UNPUBLISHED -- Rights reserved under the copyright laws of the United
  6. # States.   Use of a copyright notice is precautionary only and does not
  7. # imply publication or disclosure.
  8. # U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  9. # Use, duplication or disclosure by the Government is subject to restrictions
  10. # as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  11. # in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  12. # in similar or successor clauses in the FAR, or the DOD or NASA FAR
  13. # Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  14. # 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  15. # THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  16. # INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  17. # DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  18. # PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  19. # GRAPHICS, INC.
  20. #
  21. # $Id: pmafm,v 2.2 1997/03/21 08:54:55 kenmcd Exp $
  22.  
  23.  
  24. _usage()
  25. {
  26.     echo "Usage: pmafm folioname [command [arg ...]]"
  27. }
  28.  
  29. _help()
  30. {
  31.     echo 'PCP Archive Folio Manager
  32.  
  33. Commands:
  34.   archives        - select all archives
  35.   archives N[,...]    - select archives with these ordinal numbers
  36.   archives name[,...]    - select archives with these names
  37.   check            - integrity check for folio
  38.   help            - this message
  39.   hosts         - select archives for all hosts (the default)
  40.   hosts hostname[,...]    - select archives for just these hosts
  41.   list [verbose]    - display folio contents
  42.   quit            - exit
  43.   remove        - echo the sh(1) command to delete all files
  44.               associated with the folio
  45.   repeat tool [arg ...]    - execute a known PCP tool on each archive in turn
  46.   replay        - replay archives using the tool that created the folio
  47.   [run] tool [arg ...]    - execute a known PCP tool on the selected archives
  48.   selections        - list selected archives
  49.   
  50. Selection:
  51.   If specified, both the "archives" and the "hosts" selection criteria
  52.   are applied as a conjunction.'
  53. }
  54.  
  55. if [ $# -lt 1 ]
  56. then
  57.     _usage
  58.     exit 1
  59. fi
  60.  
  61. if [ ! -f $1 ]
  62. then
  63.     if [ "X$1" = "X-?" ]
  64.     then
  65.     _usage
  66.     echo
  67.     _help
  68.     else
  69.     echo "pmafm: cannot open folio \"$1\""
  70.     fi
  71.     exit 1
  72. fi
  73.  
  74. _FOLIOPATH=`dirname $1`
  75. if [ "$_FOLIOPATH" = "." ]
  76. then
  77.     _FOLIOPATH=""
  78. elif [ -d $_FOLIOPATH ]
  79. then
  80.     _TMP=`pwd`
  81.     cd $_FOLIOPATH
  82.     _FOLIOPATH=`pwd`/
  83.     cd $_TMP
  84. fi
  85. _FOLIO="$1"
  86. _FOLIONAME=`basename $_FOLIO`
  87.  
  88. #
  89. # TODO ... when /etc/magic is safe, use file(1)
  90. if grep '^PCPFolio' $1 >/dev/null && grep '^Version:' $1 >/dev/null
  91. then
  92.     :
  93. else
  94.     echo "pmafm: \"${_FOLIOPATH}$_FOLIONAME\" is not in PCP archive folio format"
  95.     exit 1
  96. fi
  97.  
  98. _HOSTS=""
  99. _ALL_HOSTS=`nawk <$_FOLIO '$1 == "Archive:" { printf " %s",$2 }'; echo | sort -u`
  100. _ARCHIVES=""
  101. _ALL_ARCHIVES=`nawk <$_FOLIO '$1 == "Archive:" { printf " %s",$3 }'; echo`
  102. _DEBUG=false
  103. export _DEBUG _FOLIO _HOSTS _ALL_HOSTS _ARCHIVES _ALL_ARCHIVES
  104.  
  105. _SINGLE=""
  106. _MULTI=""
  107. _REPLAY=""
  108.  
  109. # collect names of known tools, and "run" scripts for each
  110. #
  111. for config in /var/pcp/config/pmafm/*
  112. do
  113.     if [ "$config" = '/var/pcp/config/pmafm/*' ]
  114.     then
  115.     echo "pmafm: Warning: no PCP tool configurations in /var/pcp/config/pmafm/"
  116.     else
  117.     SINGLE=""
  118.     MULTI=""
  119.     REPLAY=""
  120.     . $config
  121.     [ ! -z "$SINGLE" ] && _SINGLE="$_SINGLE $SINGLE"
  122.     [ ! -z "$MULTI" ] && _MULTI="$_MULTI $MULTI"
  123.     [ ! -z "$REPLAY" ] && _REPLAY="$_REPLAY $REPLAY"
  124.     fi
  125. done
  126.  
  127. tmp=/tmp/$$
  128. trap "rm -f $tmp; exit" 0 1 2 3 15
  129.  
  130. _check_file()
  131. {
  132.     if [ ! -f "$1" ]
  133.     then
  134.     echo "No such file: $1" >>$tmp
  135.     else
  136.     dd ibs=1 count=7 if="$1" 2>/dev/null | od -X | nawk '
  137. NR == 1 && $2 == "00000084" && $3 == "50052600" { exit 0 }
  138.                         { exit 1 }'
  139.     if [ $? -eq 0 ]
  140.     then
  141.         :
  142.     else
  143.         echo "Not a PCP archive file: $1" >>$tmp
  144.     fi
  145.     fi
  146. }
  147.  
  148. _check()
  149. {
  150.     sed -n '/^Archive:/p' <$_FOLIO \
  151.     | while read xxx host arch
  152.     do
  153.     rm -f $tmp
  154.     path=`_fix_arch_path "" $arch`
  155.     echo "Archive: $path ... \c"
  156.     _check_file $path.index
  157.     _check_file $path.meta
  158.     _check_file $path.0
  159.     if [ ! -s $tmp ]
  160.     then
  161.         realhost=`pmdumplog -l $path | sed -n -e '/^Performance metrics/s/.* host //p'`
  162.         [ "X$host" != "X$realhost" ] && \
  163.         echo "Hostname mismatch: folio=$host archive=$realhost" >>$tmp
  164.     fi
  165.     if [ -s $tmp ]
  166.     then
  167.         echo "Errors"
  168.         sed -e 's/^/    /' $tmp
  169.     else
  170.         echo "OK"
  171.     fi
  172.     done
  173. }
  174.  
  175. _dir()
  176. {
  177.     echo
  178.     echo "PCP Archive Folio: $_FOLIONAME"
  179.     [ ! -z "$_FOLIOPATH" ] && echo "Folio Directory: $_FOLIOPATH"
  180.     nawk <$_FOLIO '
  181. $1 == "Created:"    { print; next }
  182. $1 == "Creator:"    { print; next }'
  183.     echo
  184.     echo "Ordinal Hostname             Archive Basename"
  185.     i=1
  186.     sed -n '/^Archive:/p' $_FOLIO \
  187.     | while read xxx host archive
  188.     do
  189.     printf "  [%3s] %-20s %s\n" $i $host $archive
  190.     [ "X$1" = "Xverbose" ] && pmdumplog -l `_fix_arch_path "" $archive` | sed -e 's/^/    /'
  191.     i=`expr $i + 1`
  192.     done
  193. }
  194.  
  195. # $1 is separater
  196. #
  197. _fix_arch_path()
  198. {
  199.     sep="$1"
  200.     shift
  201.     archlist=""
  202.     for arch
  203.     do
  204.     if [ ! -z "$_FOLIOPATH" ]
  205.     then
  206.         case $arch
  207.         in
  208.         /*)
  209.             ;;
  210.         *)
  211.             arch=${_FOLIOPATH}$arch
  212.             ;;
  213.         esac
  214.     fi
  215.     if [ -z "$archlist" ]
  216.     then
  217.         archlist="${arch}"
  218.     else
  219.         archlist="${archlist}${sep}${arch}"
  220.     fi
  221.     done
  222.  
  223.     echo $archlist
  224. }
  225.  
  226. _get_archlist()
  227. {
  228.     if [ -z "$_ARCHIVES" ]
  229.     then
  230.     archlist="$_ALL_ARCHIVES"
  231.     else
  232.     archlist="$_ARCHIVES"
  233.     fi
  234.  
  235.     if [ ! -z "$_HOSTS" ]
  236.     then
  237.     newlist=""
  238.     for arch in $archlist
  239.     do
  240.         host=`nawk <$_FOLIO '$3 == "'$arch'" { print $2; exit }'`
  241.         if echo "$_HOSTS " | grep " $host " >/dev/null
  242.         then
  243.         newlist="$newlist $arch"
  244.         fi
  245.     done
  246.     archlist=$newlist
  247.     fi
  248.  
  249.     echo $archlist
  250. }
  251.  
  252. _replay()
  253. {
  254.     _CREATOR=""
  255.     _REPLAY_CONFIG=""
  256.     eval `sed -n <$_FOLIO -e '/^Creator:/{
  257. s/Creator:[     ]*//
  258. s/^/_CREATOR=/
  259. s/[     ][     ]*/ _REPLAY_CONFIG=/
  260. p
  261. }'`
  262.     if [ -z "$_CREATOR" ]
  263.     then
  264.     echo "Error: cannot determine folio creator"
  265.     return
  266.     else
  267.     if [ ! -z "$_REPLAY_CONFIG" ]
  268.     then
  269.         _REPLAY_CONFIG=`_fix_arch_path "" "$_REPLAY_CONFIG"`
  270.         echo "Configuration File: $_REPLAY_CONFIG"
  271.         if [ ! -f "$_REPLAY_CONFIG" ]
  272.         then
  273.         echo "Error: cannot find Configuration File"
  274.         return
  275.         fi
  276.     fi
  277.     fi
  278.  
  279.     if echo "$_REPLAY " | grep " $_CREATOR " >/dev/null
  280.     then
  281.     _run $_CREATOR -c $_REPLAY_CONFIG
  282.     else
  283.     echo "Error: I don't know how to replay a folio created by \"$_CREATOR\""
  284.     echo "       ... choose a PCP tool and use the \"run\" command to replay"
  285.     fi
  286. }
  287.  
  288. _known()
  289. {
  290.     echo "Known PCP tools:"
  291.     echo "$_SINGLE$_MULTI" \
  292.     | sed -e 's/^ *//' \
  293.     | tr ' ' '\012' \
  294.     | sort \
  295.     | fmt \
  296.     | sed -e 's/^/    /'
  297. }
  298.  
  299. _run()
  300. {
  301.     tool=$1
  302.     shift
  303.     archlist=`_get_archlist`
  304.     if [ -z "$archlist" ]
  305.     then
  306.     echo "Error: no selected archives"
  307.     return
  308.     fi
  309.  
  310.     if $_REPEAT || ( echo "$_SINGLE " | grep " $tool " >/dev/null )
  311.     then
  312.     n_arch=`echo $archlist | wc -w | sed -e 's/  *//g'`
  313.     [ "$n_arch" -gt 1 ] && echo "Note: running $tool serially, once per archive"
  314.     for arch in $archlist
  315.     do
  316.         nawk <$_FOLIO '$3 == "'$arch'" { printf "Host: %s",$2; exit }'
  317.         path=`_fix_arch_path "" "$arch"`
  318.         echo " Archive: $path"
  319.         # arrgh ... special case for pmdumplog where -a is something else
  320.         # and archive comes last ... sigh
  321.         #
  322.         case $tool
  323.         in
  324.         *pmdumplog*)
  325.             $tool $* $path
  326.             ;;
  327.         
  328.         *)
  329.             $tool -a $path $*
  330.             ;;
  331.         esac
  332.     done
  333.     elif echo "$_MULTI " | grep " $tool " >/dev/null
  334.     then
  335.     $tool -a `_fix_arch_path "," $archlist` $*
  336.     elif [ "X$tool" = "X_show_me_" ]
  337.     then
  338.     for arch in $archlist
  339.     do
  340.         path=`_fix_arch_path "" "$arch"`
  341.         echo "Archive: $path"
  342.     done
  343.     else
  344.     echo "Sorry, don't know how to run \"$tool\" ..."
  345.     fi
  346. }
  347.  
  348. _MORE=true
  349. while $_MORE
  350. do
  351.     if [ $# -gt 1 ]
  352.     then
  353.     shift
  354.     cmd="$1"
  355.     shift
  356.     args=$*
  357.     _MORE=false
  358.     else
  359.     echo "pmafm> \c"
  360.     read cmd args
  361.     if [ $? -ne 0 ]
  362.     then
  363.         echo
  364.         break
  365.     elif [ -z "$cmd" ]
  366.     then
  367.         continue
  368.     fi
  369.     fi
  370.  
  371.     _REPEAT=false
  372.     if echo "$_SINGLE $_MULTI " | grep " $cmd " >/dev/null
  373.     then
  374.     # recognized command
  375.     _run $cmd $args
  376.     else
  377.     case $cmd
  378.     in
  379.  
  380.         a|ar|arch|archi|archiv|archive|archives)
  381.             case $args
  382.             in
  383.             '')
  384.                 _ARCHIVES=""
  385.                 ;;
  386.             *)
  387.                 _ARCHIVES=""
  388.                 for arch in `echo $args | sed -e 's/,/ /g'`
  389.                 do
  390.                 _TMP=`nawk <$_FOLIO '
  391.     $1 == "Archive:"    { if ("'$arch'" == $3) {
  392.                 print $3
  393.                 exit
  394.                   }
  395.                   i++
  396.                   if ("'$arch'" == i) {
  397.                 print $3
  398.                 exit
  399.                   }
  400.                 }'`
  401.                 if [ -z "$_TMP" ]
  402.                 then
  403.                     echo "Warning: archive \"$arch\" not in folio ... ignored"
  404.                 else
  405.                     _ARCHIVES="$_ARCHIVES $_TMP"
  406.                 fi
  407.                 done
  408.                 ;;
  409.             esac
  410.             ;;
  411.  
  412.         c|ch|che|chec|check)
  413.             _check
  414.             ;;
  415.  
  416.         d|de|deb|debu|debug)
  417.             if $_DEBUG
  418.             then
  419.             _DEBUG=false
  420.             set -
  421.             else
  422.             _DEBUG=true
  423.             set -x
  424.             fi
  425.             ;;
  426.  
  427.         \?|he|hel|help)
  428.             _help
  429.             ;;
  430.  
  431.         ho|hos|host|hosts)
  432.             case $args
  433.             in
  434.             '')
  435.                 _HOSTS=""
  436.                 ;;
  437.             *)
  438.                 _HOSTS=""
  439.                 for host in `echo $args | sed -e 's/,/ /g'`
  440.                 do
  441.                 if echo "$_ALL_HOSTS " | grep " $host " >/dev/null
  442.                 then
  443.                     _HOSTS="$_HOSTS $host"
  444.                 else
  445.                     echo "Warning: host \"$host\" not in folio ... ignored"
  446.                 fi
  447.                 done
  448.                 ;;
  449.             esac
  450.             ;;
  451.  
  452.         l|li|lis|list)
  453.             case $args
  454.             in
  455.             v|ve|ver|verb|verbo|verbos|verbose)
  456.                 _dir verbose
  457.                 ;;
  458.             '')
  459.                 _dir
  460.                 ;;
  461.             *)
  462.                 echo "Illegal option \"$args\" ... ignored"
  463.                 _dir
  464.                 ;;
  465.             esac
  466.             [ ! -z "$_HOSTS" ] && echo "Host Selections:$_HOSTS"
  467.             [ ! -z "$_ARCHIVES" ] && echo "Archive Selections:$_ARCHIVES"
  468.             ;;
  469.  
  470.         q|qu|qui|quit)
  471.             break
  472.             ;;
  473.  
  474.         ru|run|repe|repea|repeat)
  475.             case $cmd
  476.             in
  477.             repe|repea|repeat)
  478.             _REPEAT=true
  479.             ;;
  480.             esac
  481.             tool=`echo "$args" | sed -e 's/ .*//'`
  482.             if [ -z "$tool" ]
  483.             then
  484.             echo "Error: missing PCP tool name"
  485.             _known
  486.             elif [ "X$tool" = 'X?' ]
  487.             then
  488.             _known
  489.             elif echo "$_SINGLE $_MULTI " | grep " $tool " >/dev/null
  490.             then
  491.             _run $args
  492.             else
  493.             echo "Error: Unknown PCP tool: $tool"
  494.             _known
  495.             fi
  496.             ;;
  497.  
  498.         rem|remo|remov|remove)
  499.             [ ! -z "$_FOLIOPATH" ] && echo "( cd $_FOLIOPATH; \c"
  500.             echo "rm -f $_FOLIONAME\c"
  501.             nawk <$_FOLIO '$1 == "Creator:" && NF > 2    { printf " %s",$3; exit }'
  502.             for arch in $_ALL_ARCHIVES
  503.             do
  504.             echo " $arch.meta $arch.index\c"
  505.             path=`_fix_arch_path "" $arch`
  506.             pmdumplog -t $path 2>/dev/null \
  507.             | nawk '/^[0-9][0-9]:/ { print $2 }' \
  508.             | sort -u \
  509.             | while read vol
  510.             do
  511.                 echo " $arch.$vol\c"
  512.             done
  513.             done
  514.             [ ! -z "$_FOLIOPATH" ] && echo " )\c"
  515.             echo
  516.             ;;
  517.  
  518.         repl|repla|replay)
  519.             _replay
  520.             ;;
  521.  
  522.         s|se|sel|sele|selec|select|selecti|selectio|selection|selections)
  523.             _run _show_me_
  524.             ;;
  525.  
  526.         *)
  527.             echo "Unknown command \"$cmd\" ... enter \"help\" for more information"
  528.             ;;
  529.     esac
  530.     fi
  531. done
  532.  
  533.